﻿using System.Collections.Generic;
using System.Linq;
using GIMS.Def.Arguments;
using GIMS.Def.Flags;
using GIMS.Def.Operators;
using GIMS.Def.Settings;
using GIMS.GUI.GraphItems;

namespace GIMS.GUI.GraphManagement
{
    public class ScriptCreator
    {
        private MainWindowViewModel mainWindowViewModel;

        public ScriptCreator(MainWindowViewModel mWVM)
        {
            mainWindowViewModel = mWVM;
        }

        public string GetScript(CustomVertex vertex, out bool isExecutable)
        {
            string script = ConstructScript(vertex, out isExecutable);
            if (mainWindowViewModel.CurrentProgram == Program.mogrify && isExecutable)
            {
                script = changeLoadToSave(script);
            }
            return string.Concat(GlobalSettings.Instance.CreateScriptPart(), " ", script).Trim();
        }

        public string GetScriptPreviwImageScript(CustomVertex vertex, out bool isExecutable)
        {
            return string.Concat(GlobalSettings.Instance.CreateScriptPart(), " ", ConstructScript(vertex, out isExecutable)).Trim();
        }

        private string ConstructScript(CustomVertex vertex, out bool isExecutable)
        {
            isExecutable = true;

            if (isLoadOrSaveWithoutPath(vertex)
                || 0 == mainWindowViewModel.CurrentProgram)
            {
                isExecutable = false;
                return "";
            }

            string tmpS = "";
            string script = vertex.IMOperator.CreateScriptPart();
            var parents = mainWindowViewModel.Graph.GetParents(vertex);
            foreach (CustomVertex cv in parents)
            {
                if (isLoadOrSaveWithoutPath(vertex))
                {
                    isExecutable = false;
                    return "";
                }
                else if (parents.Count() > 1 && mainWindowViewModel.Graph.HasParents(cv))
                    tmpS = string.Concat(tmpS, "( ", ConstructScript(cv, out isExecutable), " ) ");
                else
                    tmpS = string.Concat(tmpS, ConstructScript(cv, out isExecutable) + " ");
            }
            script = tmpS + script;
            return script;
        }

        private string changeLoadToSave(string script)
        {
            string result = script;
            string[] load = script.Trim().Split(' ');
            if (load.Length > 1)
            {
                result = string.Concat(script.Substring(load[0].Length + 1), " ", load[0]);
            }
            return result;
        }


        private bool isLoadOrSaveWithoutPath(CustomVertex vertex)
        {
            if (vertex.IMOperator is SaveOperator || vertex.IMOperator is LoadOperator)
            {
                PathArgument pathArg = (PathArgument)vertex.IMOperator.Argument;
                return (string.IsNullOrEmpty(pathArg.Path) && pathArg.IsParameter==false);
            }
            return false;
        }
    }
}
